<template>
  <div class="content">
    <div class="header_box">
      <div class="header_left">字典数据</div>
      <div class="header_right">
        <el-button type="primary" @click="showAddDialog">新增</el-button>
      </div>
    </div>
    <el-table :data="dictionaryData" style="width: 100%">
      <el-table-column prop="code" label="字典编码" />
      <el-table-column prop="label" label="字典标签" />
      <el-table-column prop="value" label="字典键值" />
      <el-table-column label="状态">
        <template #default="{ row }">
          <el-tag
            :type="row.status === '1' ? 'success' : 'danger'"
            disable-transitions
          >
            {{ row.status === "1" ? "启用" : "禁用" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" />
      <el-table-column label="操作">
        <template #default="{ row }">
          <div class="icon_content">
            <el-tooltip
              class="box-item"
              effect="dark"
              content="字典的"
              placement="top-start"
            >
              <div class="icon_box">
                <el-icon @click="edit(row)" size="20" color="#71778d"
                  ><EditPen
                /></el-icon>
              </div>
            </el-tooltip>
            <el-tooltip
              class="box-item"
              effect="dark"
              content="删除"
              placement="top-start"
            >
              <div class="icon_box_d">
                <el-icon @click="remove(row)" size="20" color="#71778d"
                  ><Remove
                /></el-icon>
              </div>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑对话框 -->
    <el-dialog v-model="dialogVisible" title="编辑字典数据">
      <el-form :model="currentRow">
        <el-form-item label="字典标签">
          <el-input v-model="currentRow.label"></el-input>
        </el-form-item>
        <el-form-item label="字典键值">
          <el-input v-model="currentRow.value"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="currentRow.status" placeholder="请选择状态">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="currentRow.remark"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </template>
    </el-dialog>

    <!-- 新增对话框 -->
    <el-dialog v-model="addDialogVisible" title="新增字典数据">
      <el-form :model="newRow">
        <el-form-item label="字典标签">
          <el-input v-model="newRow.label"></el-input>
        </el-form-item>
        <el-form-item label="字典键值">
          <el-input v-model="newRow.value"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="newRow.status" placeholder="请选择状态">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="newRow.remark"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveNew">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import * as dictionaryDataApi from "../../../api/project3/dictionary/dictionary.js";
import confirm from "../../../utils/ElMessageUtils.js";

const route = useRoute();
const dictionaryData = ref([]);
const dialogVisible = ref(false);
const addDialogVisible = ref(false);
const currentRow = ref({});
const newRow = ref({ label: "", value: "", remark: "" });

const fetchDictionaryData = async () => {
  const id = route.params.id;
  console.log(`Fetching data for id: ${id}`);

  // 示例数据，根据传递的类型进行过滤或获取
  const allData = [
    {
      type: "sys_user_sex",
      code: "001",
      label: "男",
      value: "1",
      status: "1",
      remark: "男性",
    },
    {
      type: "sys_user_sex",
      code: "002",
      label: "女",
      value: "2",
      status: "1",
      remark: "女性",
    },
    {
      type: "sys_show_hide",
      code: "003",
      label: "显示",
      value: "1",
      status: "1",
      remark: "显示状态",
    },
    {
      type: "sys_show_hide",
      code: "004",
      label: "隐藏",
      value: "0",
      status: "1",
      remark: "隐藏状态",
    },
    {
      type: "sys_level",
      code: "005",
      label: "初级",
      value: "0",
      status: "1",
      remark: "",
    },
    {
      type: "sys_level",
      code: "006",
      label: "中级",
      value: "1",
      status: "1",
      remark: "",
    },
    {
      type: "sys_level",
      code: "007",
      label: "高级",
      value: "2",
      status: "0",
      remark: "",
    },
  ];

  // 过滤出当前类型的数据
  dictionaryData.value = allData; // 直接将示例数据赋值给 dictionaryData
};

const showAddDialog = () => {
  newRow.value = { label: "", value: "", remark: "" };
  addDialogVisible.value = true;
};

const saveNew = () => {
  console.log("保存新增:", newRow.value);
  dictionaryData.value.push({
    ...newRow.value,
    code: `00${dictionaryData.value.length + 1}`,
  });
  addDialogVisible.value = false;
};

const edit = (row) => {
  console.log("编辑:", row);
  currentRow.value = { ...row }; // 复制当前行数据到 currentRow
  dialogVisible.value = true; // 显示对话框
};

const saveEdit = () => {
  console.log("保存编辑:", currentRow.value);
  const index = dictionaryData.value.findIndex(
    (item) => item.code === currentRow.value.code
  );
  if (index > -1) {
    dictionaryData.value[index] = { ...currentRow.value }; // 更新数据
  }
  dialogVisible.value = false; // 关闭对话框
};

const remove = async (row) => {
  const b = await confirm("您确定要删除吗？");
  if (!b) {
    return;
  }
  const res = await dictionaryDataApi.remove({ id: row.id });
  const { code, msg } = res;
  if (code == "200") {
    ElMessage.success("操作成功");
    init();
    return;
  }
  ElMessage.error(msg);
};

const handleTabClick = (tab) => {
  console.log("Tab clicked:", tab);
  // 可以在这里处理 tab 切换逻辑
};

const exampleData = ref([
  {
    type: "sys_user_sex",
    code: "001",
    label: "男",
    value: "1",
    remark: "男性",
  },
  {
    type: "sys_user_sex",
    code: "002",
    label: "女",
    value: "2",
    remark: "女性",
  },
  {
    type: "sys_show_hide",
    code: "003",
    label: "显示",
    value: "1",
    remark: "显示状态",
  },
  {
    type: "sys_show_hide",
    code: "004",
    label: "隐藏",
    value: "0",
    remark: "隐藏状态",
  },
]);

onMounted(() => {
  fetchDictionaryData();
});
</script>

<style lang="less" scoped>
.header_right {
  background-color: #ffffff;
  justify-content: flex-end;
  border-radius: 0px 15px 0px 0px;
}
:deep(.form_box) {
  width: 80%;
  height: 40%;
  margin: 0px;
  display: flex;
  justify-content: flex-end;
}
.header_left {
  border-radius: 15px 0px 0px 0px;
  font-weight: 700;
}
.clickable {
  color: #409eff;
  cursor: pointer;
}
.example-data {
  margin-top: 20px;
  background-color: #f9f9f9;
  padding: 10px;
  border-radius: 5px;
}
</style>